<template>
  <div class="announcement-page">
    <TopBar />
    <div class="content">
      <MenuLine />

      <div class="announcement-section">
        <div class="announcement-header">
          <h2>公告信息</h2>
        </div>
        <el-card shadow="hover">
          <el-collapse>
            <el-collapse-item
              v-for="announcement in announcementList"
              :key="announcement.id"
              :title="announcement.title"
            >
              <div>{{ announcement.content }}</div>
            </el-collapse-item>
          </el-collapse>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { useAnnouncementStore } from '@/stores/announcement';
import TopBar from '@/components/TopBar.vue';
import MenuLine from '@/components/MenuLine.vue';

const announcementStore = useAnnouncementStore();
const announcementList = ref([]);

onMounted(async () => {
  try {
    const data = await announcementStore.fetchAnnouncements();
    announcementList.value = data;
  } catch (error) {
    ElMessage.error('加载公告失败: ' + error.message);
  }
});
</script>

<style scoped>
.announcement-page {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.content {
  padding-top: 60px; /* 留出顶栏高度 */
  width: 80%;
  margin: 0 auto;
  padding-bottom: 60px; /* 底部留白 */
}

.announcement-section {
  margin: 20px 0;
}

.announcement-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.announcement-header h2 {
  font-size: 24px;
  color: #333;
  flex-grow: 1;
  text-align: center;
}

.announcement-section .el-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.el-collapse {
  border: none;
  background-color: transparent;
}

.el-collapse-item__header {
  font-size: 16px;
  color: #333;
  padding: 10px 20px;
  background-color: transparent;
  border-bottom: 1px solid #ebeef5;
}

.el-collapse-item__content {
  padding: 10px 20px;
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}
</style>